package fr.appliClient.client.widget.utils.widget;

import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;

import fr.appliClient.client.widget.utils.widget.errorsmessages.ErrorDecoratedPopupPanel;
import fr.appliClient.client.widget.utils.widget.errorsmessages.ErrorLabel;

/**
 * Helper for validation action.
 * @author renavarr
 *
 */
public class ValidationHelper {

	
	/**
	 * Draw pop up with a message and change navigation after closing.
	 * @param message to display.
	 * @param navigation after close.
	 */
	public static void afterCreateAction(final String message, final String navigation) {

		DecoratedPopupPanel decoratedPopupPanel = new DecoratedPopupPanel(true);
		decoratedPopupPanel.setWidth("150px"); //$NON-NLS-1$
		decoratedPopupPanel.setWidget(new Label(message, true));
		decoratedPopupPanel.setAnimationEnabled(true);
		decoratedPopupPanel.ensureDebugId("popUpMessage");
		decoratedPopupPanel.center();
		// Navigation
		decoratedPopupPanel.addCloseHandler(new CloseHandler<PopupPanel>() {

			@Override
			public void onClose(CloseEvent<PopupPanel> arg0) {

				if (null != navigation && navigation.length() > 0) {
					History.newItem(navigation);
				}

			}
		});
	}
	
	/**
	 * Display an error pop up with the message.
	 * @param message the message to display.
	 */
	public static void errorPopup(final String message) {
		ErrorDecoratedPopupPanel decoratedPopupPanel = new ErrorDecoratedPopupPanel(true, new ErrorLabel(message, false));
		decoratedPopupPanel.center();
	}
}
